package com.tm.chargecar.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.chargecar.dto.SiteBillDto;
import com.tm.chargecar.entity.SiteDogcartEntity;
import com.tm.chargecar.mapper.ChargingPileTwoWheelersMappr;
import com.tm.chargecar.mapper.SiteDogcartMapper;
import com.tm.chargecar.result.Result;
import com.tm.chargecar.service.SiteDogcartService;
import com.tm.chargecar.utils.PageUtil;
import com.tm.chargecar.vo.SiteDogcartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author wangJie
 * @create 2022-08-24 14:10
 */
@Service
public class SiteDogcartServiceImpl extends ServiceImpl<SiteDogcartMapper, SiteDogcartEntity> implements SiteDogcartService {


    @Autowired
    private SiteDogcartMapper siteDogcartMapper;

    @Autowired
    private ChargingPileTwoWheelersMappr chargingPileTwoWheelersMappr;
    @Override
    public Result querySite(SiteDogcartVo sv) {
        //查询所有站点
        List<SiteDogcartEntity> list = siteDogcartMapper.querySite();
        //循环添加设备数
        for (int i = 0; i < list.size(); i++) {
           Long num = chargingPileTwoWheelersMappr.queryBySiteNum(list.get(i).getSiteNum());
           list.get(i).setSiteDogcartnum(num);
           Long sum = num*10;
           list.get(i).setSiteInterface(sum);
            siteDogcartMapper.updateSite(list.get(i));
        }
        PageUtil pageUtil = new PageUtil();
        Long totalCount = siteDogcartMapper.queryCount();
        pageUtil.setTotalCount(totalCount);
        List<SiteDogcartEntity> data = siteDogcartMapper.querySitePage(sv);
        pageUtil.setData(data);
        return Result.SUCCESS(data);
    }

    @Override
    public void insertSite(SiteDogcartEntity siteDogcartEntity) {
        long newTime = System.currentTimeMillis();
        siteDogcartEntity.setSiteOnlinetime(newTime);
        siteDogcartMapper.insertSite(siteDogcartEntity);

    }

    @Override
    public void updateState(SiteDogcartEntity siteDogcartEntity) {
        if(siteDogcartEntity.getSiteState()==1){
            siteDogcartEntity.setSiteState(0);
        }else {
            siteDogcartEntity.setSiteState(1);
        }
        siteDogcartMapper.updateState(siteDogcartEntity);
    }

    @Override
    public void updateAll(SiteDogcartEntity siteDogcartEntity) {
        siteDogcartMapper.updateAll(siteDogcartEntity);
    }

    @Override
    public List<SiteBillDto> queryBySiteNum(SiteDogcartEntity sitNum) {
       List<SiteBillDto> list = siteDogcartMapper.queryBySiteNum(sitNum);

        return list;
    }


}
